package mamma.dao;

import java.util.List;

import mamma.common.MybatisConfig;
import mamma.vo.FriendUpdateVo;
import mamma.vo.MemberVo;
import mamma.vo.PhotoVo;
import mamma.vo.SearchVo;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class jinkyuDao {
	private SqlSessionFactory factory;
	
	public jinkyuDao(){
		factory = MybatisConfig.getSqlSessionFactory();
	}
	
	// search and check the email
	public boolean emailCheck(String email){
		boolean result = true;			// true means that our database doesn't have this email.
		
		SqlSession session = factory.openSession();
		String searchedEmail = session.selectOne("jinkyu.emailCheck", email);
		session.close();
		
		// if it is searched on our database, return false.
		if(searchedEmail != null) result = false;
		
		return result;
	}
	
	// insert the account
	public void insertAccount(MemberVo vo){
		SqlSession session = factory.openSession();
		session.insert("jinkyu.insertAccount", vo);
		session.commit();
		
		session.insert("jinkyu.insertOwnFriends", vo);
		session.commit();
		session.close();
	}
	
	// select the account
	public MemberVo selectAccount(MemberVo vo){
		SqlSession session = factory.openSession();
		System.out.println(vo);
		MemberVo tempVo = session.selectOne("jinkyu.selectAccount", vo);
		session.close();
		
		return tempVo;
	}
	
	// select the user's photos
	public List<PhotoVo> selectPhoto(PhotoVo vo){
		SqlSession session = factory.openSession();
		List<PhotoVo> list = session.selectList("jinkyu.selectPhoto", vo);
		session.close();
		
		return list;
	}
	
	// search the member
	public List<SearchVo> searchMember(String keyword){
		SqlSession session = factory.openSession();
		List<SearchVo> list = session.selectList("jinkyu.searchMember", keyword);
		session.close();
		
		return list;
	}
	
	// search members detail
	public List<SearchVo> searchMemberDetail(SearchVo vo){
		SqlSession session = factory.openSession();
		List<SearchVo> list = session.selectList("jinkyu.searchMemberDetail", vo);
		session.close();
		
		return list;
	}
	
	// the requests that the user has.
	public List<FriendUpdateVo> selectFriendsAlerts(String email){
		SqlSession session = factory.openSession();
		List<FriendUpdateVo> list = session.selectList("jinkyu.selectFriendsAlerts", email);
		session.close();
		
		return list;
	}
	
	// the requests that I send.
	public List<FriendUpdateVo> selectMyRequest(String email){
		SqlSession session = factory.openSession();
		List<FriendUpdateVo> list = session.selectList("jinkyu.selectMyRequest", email);
		session.close();
		
		return list;
	}
	
	// allow the friend request.
	public int friendAllow(FriendUpdateVo vo){
		SqlSession session = factory.openSession();
		int result = session.update("jinkyu.friendAllow", vo);
		session.commit();
		session.close();
		
		return result;
	}
	
	// send a request
	public int sendRequest(FriendUpdateVo vo){
		SqlSession session = factory.openSession();
		int result = session.insert("jinkyu.sendRequest", vo);
		session.commit();
		session.close();
		
		return result;
	}
	
	// check it if the user send a request
	public FriendUpdateVo checkRequest(FriendUpdateVo vo){
		SqlSession session = factory.openSession();
		FriendUpdateVo temp = session.selectOne("jinkyu.checkRequest", vo);
		session.close();
		
		return temp;
	}

}
